package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.KingBdWuMapper;
import com.ruoyi.system.domain.KingBdWu;
import com.ruoyi.system.service.IKingBdWuService;

/**
 * 物料Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-28
 */
@Service
public class KingBdWuServiceImpl implements IKingBdWuService 
{
    @Autowired
    private KingBdWuMapper kingBdWuMapper;

    /**
     * 查询物料
     * 
     * @param id 物料ID
     * @return 物料
     */
    @Override
    public KingBdWu selectKingBdWuById(Long id)
    {
        return kingBdWuMapper.selectKingBdWuById(id);
    }

    /**
     * 查询物料列表
     * 
     * @param kingBdWu 物料
     * @return 物料
     */
    @Override
    public List<KingBdWu> selectKingBdWuList(KingBdWu kingBdWu)
    {
        return kingBdWuMapper.selectKingBdWuList(kingBdWu);
    }

    /**
     * 新增物料
     * 
     * @param kingBdWu 物料
     * @return 结果
     */
    @Override
    public int insertKingBdWu(KingBdWu kingBdWu)
    {
        return kingBdWuMapper.insertKingBdWu(kingBdWu);
    }

    /**
     * 修改物料
     * 
     * @param kingBdWu 物料
     * @return 结果
     */
    @Override
    public int updateKingBdWu(KingBdWu kingBdWu)
    {
        return kingBdWuMapper.updateKingBdWu(kingBdWu);
    }

    /**
     * 批量删除物料
     * 
     * @param ids 需要删除的物料ID
     * @return 结果
     */
    @Override
    public int deleteKingBdWuByIds(Long[] ids)
    {
        return kingBdWuMapper.deleteKingBdWuByIds(ids);
    }

    /**
     * 删除物料信息
     * 
     * @param id 物料ID
     * @return 结果
     */
    @Override
    public int deleteKingBdWuById(Long id)
    {
        return kingBdWuMapper.deleteKingBdWuById(id);
    }

    @Override
    public int getNum(String fId){ return  kingBdWuMapper.getNum(fId);}

    @Override
    public List<String> getUserList(){return kingBdWuMapper.getUserList();};

    @Override
    public List<KingBdWu> getNameNull(){ return kingBdWuMapper.getNameNull();}

    @Override
    public List<Map<String, String>> getGroupList(String fCompany){ return kingBdWuMapper.getGroupList(fCompany);}

    @Override
    public String getWuLists(String fCompany){
        JSONArray jsonArray = new JSONArray();
        List<Map<String, String>> list = getGroupList(fCompany);
        if(list.size()>0){
             for(Map<String, String> map:list){
                 JSONObject jsonObject1 = new JSONObject();
                 JSONArray jsonArray1 = new JSONArray();
                 KingBdWu kingBdWu = new KingBdWu();
                 kingBdWu.setfGroupCode(map.get("code"));
                 List<KingBdWu> list1 = selectKingBdWuList(kingBdWu);
                 if(list1.size()>0){
                     for (KingBdWu kingBdWu1 :list1){
                         JSONObject jsonObject2 = new JSONObject();
                         jsonObject2.put("id",kingBdWu1.getfCode());
                         jsonObject2.put("name",kingBdWu1.getfName());
                         jsonObject2.put("pid",kingBdWu1.getfGroupCode());
                         jsonArray1.add(jsonObject2);
                     }
                 }
                 jsonObject1.put("childs",jsonArray1);
                 jsonObject1.put("id",map.get("code"));
                 jsonObject1.put("name",map.get("name"));
                 jsonObject1.put("pid","0");
                 jsonArray.add(jsonObject1);
             }
        }
        return jsonArray.toString();
    }

    @Override
    public String fIdtoName(String fId){ return  kingBdWuMapper.fIdtoName(fId);}
}
